<template>
	<view class="page">
		<!-- 导航部分 -->
		<app-headers></app-headers>
		<!-- #ifdef H5 -->
		<view class="paddtb20"></view>
		<!-- #endif -->
		<view class="paddlr30">
			<text class="cuIcon-back" @click="onBack"></text>
		</view>


		<view class="yz">
			<text class="fo-gray ml10">新密码</text>
			<view class="form-input mt20">
				<input class="inp" placeholder="输入8-16位数字与字母组合的密码" v-model="ResetPwd" />
				<text class="pl50 cuIcon-close" @click="ResetPwd=''" v-show="ResetPwd.length>0"></text>
			</view>
			<view class="flex flex-col wid1p ali-cen relative mt40">
				<button class="cu-btn fo-black wid1p" :class="{btnActive:isVerificationOk}" @click="onSubmit">确定</button>
			</view>
		</view>


		<u-popup v-model="popupShow" mode="center" width="90%" border-radius="14">
			<view class="popup_content">
				<text class="popup_content-title">是否确定重设密码？</text>
				<view class="flex wid1p mt50">
					<view class="popup_content-btns" @click="popupShow=false">取消</view>
					<view class="popup_content-btns-x"></view>
					<view class="popup_content-btns" @click="popupOk">确定</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		API$UpdatePwd
	} from '@/common/API/home.js'
	import JSEncrypt from 'jsencrypt/bin/jsencrypt'
	export default {
		name: "",
		data() {
			return {
				optInfo: {}, // 页面传参数据
				ResetPwd: "", // 新密码

				popupShow: false //弹窗
			}
		},
		computed: {
			// 密码格式验证是否成功
			isVerificationOk() {
				return this.$verify.pwdFormat(this.ResetPwd)
			}
		},
		onLoad(opt) {
			this.optInfo = opt
		},
		methods: {
			// 提交
			onSubmit() {
				if (!this.$verify.pwdFormat(this.ResetPwd)) return
				this.popupShow = true
			},
			// 弹窗ok
			popupOk() {
				this.popupShow = false

				let $JSEncrypt = new JSEncrypt()
				$JSEncrypt.setPublicKey(this.$pub.pubKey()) // 设置公钥
				this.ResetPassword({
					account: this.optInfo.account,
					newPwd: $JSEncrypt.encrypt(this.ResetPwd),
				})
			},
			onBack() {
				this.$pub.back()
			},
			// 重新设置密码
			async ResetPassword(Json) {
				let {
					code,
					msg,
					data
				} = await API$UpdatePwd(Json)
				if (code === 200) {
					// H5 调用会有历史记录的
					uni.reLaunch({
						url: '/pages/other/login/index'
					});
					setTimeout(() => {
						this.$pub.Toast(msg)
					}, 500)
				}

			}
		}
	}
</script>

<style scoped lang="less">
	.page {
		background-color: #f8f8f8;
		height: 100vh;
	}

	.yz {
		padding: 40rpx;
	}

	// 输入框
	.form-input {
		color: gray;
		padding: 20rpx;
		background-color: #fff;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.inp {
			flex: 1;
		}
	}

	// 按钮
	.cu-btn {
		height: 80rpx;
		background-color: #f8f8f8;
		color: #000;
		border: solid 1px #cfcfcf;
	}

	.btnActive {
		background-color: skyblue;
		color: #ffffff;
	}


	// 弹窗内容
	.popup_content {
		padding: 20rpx 0 0 0;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.popup_content-title {
		margin-top: 20rpx;
		border: none;
		font-size: 40rpx;
	}

	.popup_content-btns-x {
		height: 80rpx;
		width: 2rpx;
		background-color: rgba(0, 0, 0, 0.1);
	}

	.popup_content-btns {
		flex: 1;
		height: 100rpx;
		line-height: 100rpx;
		text-align: center;
		font-size: 36rpx;
	}

	.popup_content-btns:active {
		background-color: rgba(0, 0, 0, 0.1);
	}
</style>
